﻿/*Javascript used on masterlayout page*/

$(document).ready(function () {

    GetCourses();

});

function GetCoursePoints(ID) {

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "/Home/GetCoursePoints/" + ID,
        data: "{ }",
        dataType: "json",

        success: function (model) {
        if(model.totalPoints === null)
            model.totalPoints = 0;

            $('#courses2').append("<li><p>Heildarstigafjoldi: " + model.totalPoints + "</p></li>");
            $("#courses2").append("<li><p>Sæti: " + model.currentPosition + "</p></li>");
        }, //Success end

        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        } //Error end

    }); //Ajax end
}; //GetPoints end

function GetMainPoints() {

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "/Home/GetMainPoints/",
        data: "{ }",
        dataType: "json",

        success: function (model) {
            for(var i = 0; i < model.length; i++)
            {
             $("#courses2").append("<li><a href='/Course/Level/" + model[i].courseID + "/'>" + model[i].courseName + "</a></li>");
             $("#courses2").append("<progress max='" + model[i].maxPoints +"' value='" + model[i].userPoints  + "'></progress>");
            }
        }, //Success end

        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        } //Error end

    }); //Ajax end
}; //GetPoints end

function GetCourses() {

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "/Home/GetCourses",
        data: "{ }",
        dataType: "json",

        success: function (model) {
            var hr = "<hr />"
            var i = 0;
            for (i; i < model.length - 1; i++) {
                $('#courses').append("<li><a href='/Course/Level/" + model[i].Key + "/'>" + model[i].Value + "</a></li>" + hr);
               $('#newsMenu').append("<li><a href='/Course/Index/" + model[i].Key + "/'>" + model[i].Value + "</a></li>" + hr);
            }
            $('#courses').append("<li><a href='/Course/Level/" + model[i].Key + "/'>" + model[i].Value + "</a></li>");
             $('#newsMenu').append("<li><a href='/Course/Index/" + model[i].Key + "/'>" + model[i].Value + "</a></li>" + hr);
            
        }, //Success end

        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        } //Error end

    });    //Ajax end


}; //GetPoints end

var c = 0;

function DisikeComment(ID)
{
    $.ajax({ 
            type: "POST",
            URL: "Lecture/DislikeComment/" + ID,
            data: ID,
            success: function() {
                $("#dislike-" + ID).hide();
                var number = document.getElementById("number-" + ID).innerHTML;
                number = parseInt(number);
                $("#number-" + ID).text(number + 1);
                $("#like-link-" + ID).show();
            }
    });
}

function GetComments(ID){

                    $.ajax({
                        type: "GET",
                        contentType: "application/json; charset=utf-8",
                        url: "/Lecture/GetComments/" + ID,
                        data: "{}",
                        dataType: "json",
                        success: function (comment) {
                        for(c; c < comment.length; c++){
                                comment[c].CommentDate = parseJsonDate(comment[c].CommentDate);
                                $("#commentTemplate").tmpl(comment[c]).hide().appendTo("#comments").fadeIn("slow");
                                $("#dislike-" + comment[c].ID).hide();
                                if(comment[c].alreadyLike === 1){
                                    $("#like-link-" + comment[c].ID).hide();
                                    $("#alreadyLiked-" + comment[c].ID).append("<h5>Þér líkar þessi athugasemd!<h5>");
                                    $("#dislike-" + comment[c].ID).show();
                                    }
                                }

                        },

                        error: function (xhr, err) {
                            // Note: just for debugging purposes!
                            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                            alert("responseText: " + xhr.responseText);
                        }
                    });//ajax end
 }; //GetPoints end

  function AddComment() {

    var ID = $("#ID").val();
    var commentText = $("#commentText").val();
    var form = { "ID" : ID, "CommentText" : commentText };
    console.log(form);

     $.ajax({
        type: "POST",
        url: "/Lecture/AddComments/",
        data: form,

        success: function () {             
            GetComments(form.ID);
            document.getElementById("commentText").value = '';
        },

        error: function (xhr, err) {
        // Note: just for debugging purposes!
        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        alert("responseText: " + xhr.responseText);
        },

     });

 };

 function LikeComment(ID)
 {

    $.ajax({ 
        type: "POST",
        url: "/Lecture/LikeComment/" + ID,

        success: function() { 
            $("#like-link-" + ID).hide();
             $("#dislike-" + ID).show();
             var number = document.getElementById("number-" + ID).innerHTML;
              number = parseInt(number);
                $("#number-" + ID).text(number + 1);

        },
        error: function (xhr, err) {
        // Note: just for debugging purposes!
        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        alert("responseText: " + xhr.responseText);
        },
    
    });

 };

 function parseJsonDate(jsonDate) {
    var offset = new Date().getTimezoneOffset() * 60000;
    var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
    
    if (parts[2] == undefined) 
      parts[2] = 0;
    
    if (parts[3] == undefined) 
      parts[3] = 0;
    
    return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
    };